<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php
function get_page($page)
{
	$page = db_prepare_input($page) + 0;
	if ($page <= 0)
		$page = 1;
	return $page;
}
function db_prepare_input($string) {
	if (is_string($string)) {
		return trim(sanitize_string(stripslashes($string)));
	} elseif (is_array($string)) {
		reset($string);
		while (list($key, $value) = each($string)) {
			$string[$key] = db_prepare_input($value);
		}
		return $string;
	} else {
		return $string;
	}
}
function sanitize_string($string) {
	//$string = preg_replace(' +', ' ', $string);
	$string = preg_replace("/'/", "''", $string);
	return preg_replace("/[<>]/", '_', $string);
}
function left_string_nwords($string, $delimeter = "", $N, $concat)
{
	$arr = explode($delimeter, $string);
	if (count($arr)==1)	
	{
		return $string;
	}
	$str = "";
	for ($i=0;$i<$N;$i++)
	{
		$str.=$arr[$i].$delimeter;
	}
	$str = substr($str,0,-1);
	if(count($arr)+1>$N)
		$str=$str." ".$concat;
	return $str;
}	
function truncate_string($string, $max_chars_words, $delimeter = "", $N, $concat)
{
	$arr = explode($delimeter, $string);
	$str_new="";
	$str_temp="";
	//chuoi rong ==> return;
	if (strlen($string)==0)	
	{
		return $string;
	}
	//Tao chuoi moi (da cat chuoi dai)
	$str = "";
	for ($i=0;$i<$N;$i++)
	{
		
		$j=0;
		$str_temp = $arr[$i];
		if (strlen($arr[$i])>$max_chars_words)
		{
			while ($j<strlen($arr[$i]))
			{
				$j=$j+$max_chars_words;
				$str_temp = mb_substr_replace($str_temp, ' ',$j,0,'UTF-8');
			}
		}
		$str_new = $str_new." ".$str_temp;
	}
	//tien hanh cat chuoi that su & tra ra output
	return left_string_nwords(trim($str_new), $delimeter, $N, $concat);
}

function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
{
		if (extension_loaded('mbstring') === true)
		{
				$string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
				if ($start < 0)
				{
						$start = max(0, $string_length + $start);
				}
				else if ($start > $string_length)
				{
						$start = $string_length;
				}
				if ($length < 0)
				{
						$length = max(0, $string_length - $start + $length);
				}
				else if ((is_null($length) === true) || ($length > $string_length))
				{
						$length = $string_length;
				}
				if (($start + $length) > $string_length)
				{
						$length = $string_length - $start;
				}
				if (is_null($encoding) === true)
				{
						return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length);
				}
				return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
		}
		return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}	
function convertToVn($str) {
	$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
	$str = preg_replace("/(à|á|à|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/i", 'a', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ó|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(đ)/", 'd', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ặ|Ẳ|Ẵ)/", 'a', $str);
	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(É|È|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ó|Ợ|Ở|Ỡ)/", 'o', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Đ)/", 'D', $str);
	$str = preg_replace("/( )/", '-', $str);
        $str = preg_replace('/[^a-z|-]/i', '', $str);
	//$str = preg_replace("/\s*(-)\s*/", ' ', $str);
	//$str = preg_replace("/[^a-zA-Z\\d_]/", '', $str);
	$str = str_replace(
	array('(',')','',']','[','.',',',' ','%',"/","\\",'"','?','<','>',"#","^","`","'","=","!",":",",,","..","...","*","&","__","▄"),
	array('','','','','','','','','','','','','','','','','','','','','','','','','',"","","","","","",""), $str);
	$str = mb_strtolower($str);
	str_replace("_","",$str);
	return $str;
}

function convertToVn_dash($str) {
	$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
	$str = preg_replace("/(à|á|à|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/i", 'a', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ó|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(đ)/", 'd', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ặ|Ẳ|Ẵ)/", 'a', $str);
	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(É|È|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ó|Ợ|Ở|Ỡ)/", 'o', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Đ)/", 'D', $str);
	$str = preg_replace("/( )/", 'xxxxxx', $str);
	$str = preg_replace("/\s*(-)\s*/", ' ', $str);
	$str = preg_replace("/[^a-zA-Z\\d_]/", '', $str);
	$str = str_replace(
	array('(',')','-',']','[','.',',',' ','%',"/","\\",'"','?','<','>',"#","^","`","'","=","!",":",",,","..","*","&","__","▄"),
	array('_','_','_','_','_','_','_','_','' ,'' ,''  ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-','' ,'-',''  ,''  ,'' , "_" ,"" ,""), $str);
	$str = mb_strtolower($str);
	$str = str_replace("xxxxxx","-",$str);
	return $str;
}

function checkInt($string) {
	$pattern = '/^[0-9]+$/i';
	return preg_match ($pattern,$string);
}
////////// day month year commbobox
function cbo_day($name,$selected='',$params=''){
    $day=array(array('id'=>'','text'=>'Ng&agrave;y'));
    for($i=1;$i<=31;$i++){
      $day[]=array('id'=>$i,'text'=>$i);
    }
    $day=draw_selectbox($name,$day,$selected,$params);
    return $day;
}
function cbo_month($name,$selected='',$params=''){
    $month=array(array('id'=>'','text'=>'Th&aacute;ng'));
    for($i=1;$i<=12;$i++){
      $month[]=array('id'=>$i,'text'=>$i);
    }
    $month=draw_selectbox($name,$month,$selected,$params);
    return $month;
}
function cbo_year($name,$selected='',$params=''){
    $year=array(array('id'=>'','text'=>'N&#259;m'));
    for($i=2007;$i>=1920;$i--){
      $year[]=array('id'=>$i,'text'=>$i);
    }
    $year=draw_selectbox($name,$year,$selected,$params);
    return $year;
}
function draw_selectbox($name, $values, $default = '', $parameters = '') {
	$field = '<select name="' . parse_input_field_data($name, array('"' => '&quot;')) . '"';
	 
	if (not_null($parameters)) $field .= ' ' . $parameters;
	 
	$field .= '>';
	 
	for ($i = 0, $n = sizeof($values); $i < $n; $i++) {
		$field .= '<option value="' . parse_input_field_data($values[$i]['id'], array('"' => '&quot;')) . '"';
		if ($default == $values[$i]['id']) {
			$field .= ' selected';
		}
		 
		$field .= '>' . parse_input_field_data($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
	}
	$field .= '</select>';
	 
	return $field;
}
function parse_input_field_data($data, $parse) {
	return strtr(trim($data), $parse);
}
function not_null($value) {
	if (is_array($value)) {
		if (sizeof($value) > 0) {
			return true;
		} else {
			return false;
		}
	} else {
		if ( (is_string($value) || is_int($value)) && ($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0)) {
			return true;
		} else {
			return false;
		}
	}
}
////////// parse VN float
function parseVNfloat($value) {
	return str_replace(',','.',$value) + 0;
}
////////// realty type
function cbo_realtytype($name, $selected='', $params='') {
	$data_db = array(array('id'=>'','text'=>'--- Không chọn ---'));
	foreach ($params as $k=>$v) {
		$data_db[] = array('id'=>$v->realtytype_id,'text'=>$v->realtytype_name);
	}
	$data = draw_selectbox($name, $data_db, $selected, $params);
	return $data;
}
////////// direction
function cbo_direction($name, $selected='', $params='')
{
	$data_db = array(array('id'=>'','text'=>'--- Không chọn ---'));
	foreach ($params as $k=>$v) {
		$data_db[] = array('id'=>$v->direction_id,'text'=>$v->direction_name);
	}
	$data = draw_selectbox($name, $data_db, $selected, $params);
	return $data;
}
////////// license type
function cbo_licensetype($name, $selected='', $params='')
{
	$data_db = array(array('id'=>'','text'=>'--- Không chọn ---'));
	foreach ($params as $k=>$v) {
		$data_db[] = array('id'=>$v->licensetype_id,'text'=>$v->licensetype_name);
	}
	$data = draw_selectbox($name, $data_db, $selected, $params);
	return $data;
}
////////// license type
function cbo_pricetype($name, $selected='', $params=''){
	$data_db = array(array('id'=>'','text'=>'--- Không chọn ---'));
	foreach ($params as $k=>$v) {
		$data_db[] = array('id'=>$v->pricetype_id,'text'=>$v->pricetype_name);
	}
	$data = draw_selectbox($name, $data_db, $selected, $params);
	return $data;
}

function createKey($amount){
	$keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$randkey = "";
	for ($i=0; $i<$amount; $i++)
	$randkey .= substr($keyset, rand(0,strlen($keyset)-1), 1);
	return $randkey;
}

function strip_html($text)
{
	// PHP's strip_tags() function will remove tags, but it
	// doesn't remove scripts, styles, and other unwanted
	// invisible text between tags.  Also, as a prelude to
	// tokenizing the text, we need to insure that when
	// block-level tags (such as <p> or <div>) are removed,
	// neighboring words aren't joined.
	$text = preg_replace(
		array(
			// Remove invisible content
			'@<head[^>]*?>.*?</head>@siu',
			'@<style[^>]*?>.*?</style>@siu',
			'@<script[^>]*?.*?</script>@siu',
			'@<object[^>]*?.*?</object>@siu',
			'@<embed[^>]*?.*?</embed>@siu',
			'@<applet[^>]*?.*?</applet>@siu',
			'@<noframes[^>]*?.*?</noframes>@siu',
			'@<noscript[^>]*?.*?</noscript>@siu',
			'@<noembed[^>]*?.*?</noembed>@siu',

// Add line breaks before & after blocks
			'@<((br)|(hr))@iu',
			'@</?((address)|(blockquote)|(center)|(del))@iu',
			'@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
			'@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
			'@</?((table)|(th)|(td)|(caption))@iu',
			'@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
			'@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
			'@</?((frameset)|(frame)|(iframe))@iu',
			'/<div .*[",\'](\\s?)>/','/<\/div>/',
			'/<[a,A] .*[",\'](\\s?)>/','/<\/[a,A]>/',
			'/<li .*[",\'](\\s?)>/','/<\/li>/',
			'/<ul .*[",\'](\\s?)>/','/<\/ul>/',
			'/<table .*[",\'](\\s?)>/','/<\/table>/',
			'/<tr .*[",\'](\\s?)>/','/<\/tr>/',
			'/<td .*[",\'](\\s?)>/','/<\/td>/',
			'/<input .*[",\'](\\s?)\/>/','/<\/input>/',
			'/<img .*[",\'](\\s?)[\/]?>/','/<[\/]?img>/',
			'/<iframe .*[",\'](\\s?)[\/]?>/','/<[\/]?iframe>/',
			'/<link .*[",\'](\\s?)[\/]?>/',
			'/<h[1-9]>/','/<\/h[1-9]>/',
			'/<p .*[",\'](\\s?)>/','/<\/p>/',
			'/<br .*[",\'](\\s?)[\/]?>/',
			'/<label .*[",\'](\\s?)>/','/<\/label>/',
			'/<form .*[",\'](\\s?)>/','/<\/form>/',
			'/<!DOCTYPE .*[",\'](\\s?)>/',
			'/<html .*[",\'](\\s?)>/','/<\/html>/',
			'/<body>/','/<\/body>/',
			'/<tr>/','/<ul>/','/<li>/','/<p>/',
		),
		array(
			' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
			"\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
			"\n\$0", "\n\$0",
		),
		$text );

	// Remove all remaining tags and comments and return.
	return $text;
}

function ValidateEmailFull($sEmail){
	$pos = strpos($sEmail,"@");
	if ($pos === false)
		return 0;
	$arr = explode("@",$sEmail);
	$sEmailName = $arr[0];
	$sEmailDomain = $arr[1];						
	if(!preg_match("/^[a-z0-9]+([_\.]+[a-z0-9]+)*$/", $sEmailName))		
		return 0;
	if(!preg_match("/^(@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+([a-z]+([\.]{1,1}+[a-z]{2,2})*$))/","@".$sEmailDomain))
		return 0;
	return 	1;		
}

function highlightWords($text, $words) {
    $wordsArray = array();
    $markedWords = array();
    // explode the phrase in words
    $wordsArray = explode(' ', $words); 

    foreach ($wordsArray as $k => $word) {
      $markedWords[$k]='<mark>'.$word.'</mark>';
    }

    $text = str_ireplace($wordsArray, $markedWords, $text);

    //right trows results
    return $text;
}
?>